package com.sghd.candy.module.skill.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.json.JsonUtils;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Allen
 * @date 2018/12/06
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_10_MINUTE))
public class PlayerSkill implements IEntity<Long>, PerPersist {

    /**玩家Id*/
    @Id
    private Long playerId;
    /**技能碎片*/
    private Integer debris;
    /**玩家技能对应的等级*/
    @Transient
    private Map<Integer, Integer> skillLevel;
    @Lob
    private String skillLevelJson;
    /**PVP携带的技能*/
    @Type(type="com.sghd.common.ramcache.orm.type.Set")
    private Set<Integer> fightSkill = new HashSet<>();

    public Long getPlayerId() {
        return playerId;
    }

    void setPlayerId(Long playerId) {
        this.playerId = playerId;
    }

    public Integer getDebris() {
        return debris;
    }

    void setDebris(Integer debris) {
        this.debris = debris;
    }

    public Map<Integer, Integer> getSkillLevel() {
        return new HashMap<>(this.skillLevel);
    }

    void setSkillLevel(Map<Integer, Integer> skillLevel) {
        this.skillLevel = skillLevel;
    }

    public String getSkillLevelJson() {
        return skillLevelJson;
    }

    void setSkillLevelJson(String skillLevelJson) {
        this.skillLevelJson = skillLevelJson;
    }

    public Set<Integer> getFightSkill() {
        return fightSkill;
    }

    static PlayerSkill valueOf(long playerId) {
        PlayerSkill playerSkill = new PlayerSkill();
        playerSkill.setPlayerId(playerId);
        playerSkill.setSkillLevel(new HashMap<>());
        playerSkill.setFightSkill(new HashSet<>());
        playerSkill.setDebris(0);
        return playerSkill;
    }

    @Enhance
    void setFightSkill(Set<Integer> fightSkill) {
        this.fightSkill.clear();
        this.fightSkill.addAll(fightSkill);
    }

    @Enhance
    void addPlayerSkill(Set<Integer> newSkill) {
        newSkill.forEach(skillId -> this.skillLevel.put(skillId, 0));
    }

    @Enhance
    void addPlayerSkill(int skillId) {
        this.skillLevel.put(skillId, 0);
    }

    @Enhance
    void upgradeSkill(int skillId, int disCount) {
        this.debris = this.debris - disCount;
        this.skillLevel.put(skillId, this.skillLevel.get(skillId) + 1);
    }

    @Enhance
    void change(int changeNum) {
        this.debris = this.debris + changeNum;
    }

    @Enhance
    void addFightSkill(int skillId) {
        if (!getFightSkill().contains(skillId)) {
            getFightSkill().add(skillId);
        }
    }
    @Enhance
    void removeFightSkill(int skillId) {
        getFightSkill().remove(skillId);
    }

    @Override
    public Long getId() {
        return this.playerId;
    }

    @Override
    public void afterLoad() {
        skillLevel = JsonUtils.string2Map(skillLevelJson, Integer.class, Integer.class);
    }

    @Override
    public void beforePersist() {
        skillLevelJson = JsonUtils.object2String(skillLevel);
    }
}
